/*
* $Id: ChangePointView.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2009 by Volokitin Alexey
* All rights reserved
*
*/
#include "Common/include.h"
#include "ChangePointView.h"
#include "ViewFieldValuesHelper.h"
#include "Visual/Drawers/EditorDrawer/HeaderItem.h"
#include "Visual/Drawers/EditorDrawer/EditItem.h"
#include "Settings/Journal/PointViewSettings.h"
#include "Settings/DinIntDefined.h"
#include "Settings/SettingControlIds.h"
#include "Components/Journal/FamilyManager.h"

#pragma warning(disable: 4407)

#define TITLE_ID		1
#define NAME_POINT_ID	2
#define NOTE_POINT_ID	3
#define USER_FIELD_ID	4

CChangePointView::CChangePointView(CState* pState)
: CAddChangeView(pState), m_nUserFields(0){}
CChangePointView::~CChangePointView(){}

void CChangePointView::Init(){
	m_editorDrawer.Clear();
	m_editorDrawer.AddItem(new CHeaderItem(ILanguageManager::GetLanguageManager()->GetString(CHANGE_POINT_ID), TITLE_ID));

	std::tstring strName;
	std::tstring strNote;
	int nIdRecord = 0;
	CFamilyManager::point pnt = IJournalComponent::GetFamilyManager()->GetPoint(m_nIdParameter);
	m_editorDrawer.AddItem(new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_POINT_NAME_ID), NAME_POINT_ID, editItem::TEXT, pnt.m_strName));
	CEditorItem* pItemNote = new CEditItem(ILanguageManager::GetLanguageManager()->GetString(TABLE_HEADER_NOTE_ID), NOTE_POINT_ID, editItem::TEXT, pnt.m_strNote);
	m_editorDrawer.AddItem(pItemNote);
	CPointViewSettings pSettings;
	if(pSettings.GetState(CHECK_NOTE_ID) != CHECK_BOX_STATE_1)
		pItemNote->Hide(true);

	CViewFieldValuesHelper helper(&m_editorDrawer);
	m_nUserFields = helper.GenerateUserEditItems(TypeField::POINTS, USER_FIELD_ID, pnt.m_nIdRecord);
}

void CChangePointView::SetState(){
	m_pState->m_nState = Journal::VIEW_CHANGE_POINT;
}
bool CChangePointView::IsViewState(State state){
	return state.m_type == Journal::CHANGE_POINT;
}

BEGIN_MESSAGE_MAP(CChangePointView, CAddChangeView)
	ON_COMMAND(BUTTON_SAVE_ID, OnSavePoint)
END_MESSAGE_MAP()

void CChangePointView::OnSavePoint(){
	CFamilyManager* pManager = IJournalComponent::GetFamilyManager();
	CFamilyManager::point pnt;
	pnt.m_strName = m_editorDrawer.GetValueItem(NAME_POINT_ID);
	pnt.m_strNote = m_editorDrawer.GetValueItem(NOTE_POINT_ID);
	std::map<std::tstring, std::tstring> aUserFields;
	for(int i = 1; i <= m_nUserFields; i++)
		aUserFields.insert(std::make_pair(m_editorDrawer.GetNameItem(USER_FIELD_ID + i), m_editorDrawer.GetValueItem(USER_FIELD_ID + i)));
	pManager->ChangePoint(m_nIdParameter, pnt, aUserFields);
	__super::OnCancel();
}